package com.frxuexi.park2303.notice

import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.NoticeBeanItem
import com.frxuexi.base.http.Const
import com.frxuexi.base.http.WebSocketClient
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.UiState
import com.frxuexi.base.utils.DateTimeUtils
import com.frxuexi.park2303.R
import com.frxuexi.park2303.databinding.ActivityNoticeAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NoticeAddActivity")
@AndroidEntryPoint
class NoticeAddActivity : BaseActivity<ActivityNoticeAddBinding,NoticeIntent,NoticeViewModel>() {
    override fun getVM(): NoticeViewModel {
        return viewModels<NoticeViewModel>().value
    }

    override fun getBind(): ActivityNoticeAddBinding {
        return ActivityNoticeAddBinding.inflate(layoutInflater)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initData() {
        WebSocketClient.data.observe(this){
            if(!it.contains("|"))
                return@observe
            val split = it.split("|")
            val manager = getSystemService(NotificationManager::class.java)
            val channel =
                NotificationChannel("not", "发布公告", NotificationManager.IMPORTANCE_LOW)
            manager.createNotificationChannel(channel)
            val notification = NotificationCompat.Builder(this, "not")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle(split[0])
                .setContentText(split[1])
                .build()
            manager.notify(1,notification)
        }

        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("发布成功")
                        finish()
                    }
                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        WebSocketClient.start()
        binding.noticeAddHead.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("发布公告")
            headAdd.setImageResource(com.frxuexi.base.R.drawable.ic_action_ok)
            headAdd.setOnClickListener {
                val title = binding.noticeTitle.text.toString()
                val content = binding.noticeContent.text.toString()
                val type = binding.noticeType.text.toString()
                WebSocketClient.sendMessage("${title}|${content}")
                val item = NoticeBeanItem(
                    content,
                    null,
                    DateTimeUtils.getDateTime(System.currentTimeMillis()),
                    "启用",
                    title,
                    type,
                    SPUtils.getInstance().getInt(Const.UID)
                )
                vm.sendIntent(NoticeIntent.addNotice(item))
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        WebSocketClient.closeSocket()
    }

}